using Aurora.AI.SqlSugar.Framework;
using Aurora.AI.SqlSugar.Framework.Repositories;
using SqlSugar;

namespace Aurora.AI.Domain.Domain.ChatChannel.Repositories;

public sealed class ChatChannelRepository
    : Repository<Aggregates.ChatChannel>, IChatChannelRepository
{
    public Task<List<ChatChannel.Aggregates.ChatChannel>> GetListAsync(int page, int pageSize)
    {
        return GetDb().Queryable<ChatChannel.Aggregates.ChatChannel>().OrderByDescending(x => x.CreationTime)
            .ToOffsetPageAsync(page, pageSize);
    }

    public async Task<(int total, List<Aggregates.ChatChannel> list)> GetPageListAsync(int page, int pageSize)
    {
        RefAsync<int> total = 0;
        List<Aggregates.ChatChannel> offsetPageAsync = await GetDb().Queryable<ChatChannel.Aggregates.ChatChannel>().OrderByDescending(x => x.CreationTime)
            .ToOffsetPageAsync(page, pageSize, total);
        return (total, offsetPageAsync);
    }

    public Task<List<ChatChannel.Aggregates.ChatChannel>> GetListAsync()
    {
        return GetDb().Queryable<ChatChannel.Aggregates.ChatChannel>().OrderByDescending(x => x.CreationTime)
            .ToListAsync();
    }

    public Task<int> GetCountAsync()
    {
        return GetDb().Queryable<Aggregates.ChatChannel>().CountAsync();
    }

    public async Task Create(Aggregates.ChatChannel application)
    {
        application.Id = Guid.NewGuid().ToString().Replace("-", "");
        await InsertAsync(application);
    }



    public async Task<Aggregates.ChatChannel> GetByIdAsync(string id)
    {
        return await base.GetByIdAsync(id);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="application"></param>
    /// <returns></returns>
    public async Task UpdateChannel(Aggregates.ChatChannel application)
    {
        application.ModificationTime = DateTime.Now;
        await UpdateAsync(application);
    }

    public async Task<IEnumerable<Aggregates.ChatChannel>> GetByModelAsync(string model)
    {
        var list = await base.GetListAsync(x => x.IsDeleted == false);
        return list.Where(x => x.Models.Contains(model));
    }

    public ChatChannelRepository(IServiceProvider serviceProvider) : base(serviceProvider)
    {
    }
}